package entities;

import exceptions.UnrecoverableException;

import datatypes.Coordinate;
import datatypes.MCItemSlot;

public class Player extends Entity {
	
	private String name;
	
	private Coordinate spawnpoint;
	@SuppressWarnings("unused")
	private double stance = 0;
	@SuppressWarnings("unused")
	private boolean onground = true;
	
	
	/**
	 * The item-slot the user currently has selected,
	 * this is visible by other clients.
	 * Value in the range (0, 9)
	 * Its default is 0 (when the user connects).
	 */
	private int currentSlot = 0;
	
	
	/**
	 * Holding the possible user armors.
	 * null if no item is placed.
	 * (1=helmet, 2=chest, 3=legs, 4=feet)
	 */
	@SuppressWarnings("unused")
	private MCItemSlot[] armor = new MCItemSlot[4];
	
	/**
	 * Holder for the players current inventory.
	 * (the way I see it: their bag).
	 * 3 rows of 9 items, counting from top left
	 * to bottom right in lines.
	 */
	@SuppressWarnings("unused")
	private MCItemSlot[] inventory = new MCItemSlot[27]; 
	
	
	/**
	 * Holder for the users 9 held items.
	 * null means: empty. 
	 */
	private MCItemSlot[] helditems = new MCItemSlot[9];
	
	
	
	/**
	 * Create a new player. Each player has a unique EID.
	 * Also a username is required.
	 * @param eid
	 * @param name
	 */
	public Player(int eid, String name) {
		super(eid);
		this.name = name;
		
		this.spawnpoint = new Coordinate(0, 100, 0);
	}
	
	/**
	 * Returns the player name
	 * @return
	 */
	public String getName() {
		return this.name;
	}
	

	public void setOnGround(boolean onground) {
		this.onground = onground;
	}
	

	public void setStance(double stance) {
		this.stance = stance;
	}
	
	/**
	 * The current players spawnpoint.
	 * @return
	 */
	public Coordinate spawnpoint() {
		return spawnpoint;
	}
	
	
	/**
	 * Should be called when the user selects another main slot (1-9).
	 * Calling this function will update all other clients with this
	 * information.
	 * 
	 * @param slotid
	 * @throws UnrecoverableException 
	 * @throws MC 
	 */
	public void setSelectedSlot(short slotid) throws UnrecoverableException {
		if(slotid < 0 || slotid > 9) {
			throw new UnrecoverableException("Invalid slotid");
		}
		this.currentSlot = slotid;
		this.updateMe(EntityEvent.SELECTEDITEMCHANGED);
	}
	
	/**
	 * Returns the index of the slot the user currently has selected.
	 * @return
	 */
	public short getSelectedSlot() {
		return (short) this.currentSlot;
	}
	
	
	
	/**
	 * Sets the item for a given user slot (0-9).
	 * @param slotid
	 * @param slot
	 * @throws UnrecoverableException
	 */
	public void setSlotItem(short slotid, MCItemSlot slot) throws UnrecoverableException {
		if(slotid < 0 || slotid > 9) {
			throw new UnrecoverableException("Invalid slotId");
		}
		this.helditems[slotid] = slot;
		this.updateMe(EntityEvent.SELECTEDITEMCHANGED);
	}
	
	
	/**
	 * Returns the players slotitem for the requested slot.
	 * @param slotid
	 * @return
	 * @throws UnrecoverableException
	 */
	public MCItemSlot getHeldItemAtSlot(short slotid) throws UnrecoverableException {
		if(slotid < 0 || slotid > 9) {
			throw new UnrecoverableException("Invalid slotId");
		}
		return this.helditems[slotid];
	}

}
